/**
* @description: 809. 情感丰富的文字
* @author hewei
* @date 2022/8/30 15:03
* @version 1.0
*/

public class ExpressiveWords {

    public int expressiveWords(String s, String[] words) {
        int ans = 0;
        for (String word : words) {
            int p1 = 0;
            int p2 = 0;
            int count1 = 1;
            int count2 = 1;
            boolean flag = true;
            while (p1 < s.length() && p2 < word.length()) {
                while (p1 < s.length() - 1 && s.charAt(p1) == s.charAt(p1 + 1)) {
                    ++count1;
                    ++p1;
                }
                while (p2 < word.length() - 1 && word.charAt(p2) == word.charAt(p2 + 1)) {
                    ++count2;
                    ++p2;
                }
                if (count1 < count2 || (count1 > count2 && count1 < 3) || word.charAt(p2) != s.charAt(p1)) {
                    flag = false;
                    break;
                }
                count1 = 1;
                count2 = 1;
                ++p1;
                ++p2;
            }
            if (flag && p1 == s.length() && p2 == word.length()) ans++;
        }
        return ans;
    }
}
